package cn.neusoft.service.impl;

import cn.neusoft.bean.Business;
import cn.neusoft.bean.Cart;
import cn.neusoft.dao.ICartDao;
import cn.neusoft.dao.impl.CartDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.query.CartQuery;
import cn.neusoft.service.ICartService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;

import java.util.List;

public class CartService implements ICartService {
    private ICartDao cartDao;

    public CartService() {
        cartDao = new CartDao();
    }


    @Override
    public ResultBean add(Cart cart) throws Exception {
        // 参数校验
        if (cart == null ||
                cart.getFoodId() == null||
                cart.getBusinessId() == null||
                StringUtils.isEmpty(cart.getUserId())||
                cart.getQuantity() == null
        ){
            return ResultBean.ERROR(10001,"params null");
        }
        int i = cartDao.add(cart);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("add cart fail");
    }

    @Override
    public ResultBean update(Cart cart) throws Exception {
        // 参数校验
        if (cart == null ||
                cart.getCartId() == null||
                cart.getFoodId() == null||
                cart.getBusinessId() == null||
                StringUtils.isEmpty(cart.getUserId())||
                cart.getQuantity() == null
        ){
            return ResultBean.ERROR(10001,"params null");
        }
        int i = cartDao.update(cart);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("update cart fail");
    }

    @Override
    public ResultBean delete(Integer id) throws Exception {
        if (id == null){
            return ResultBean.ERROR(10001,"params null");
        }
        int i = cartDao.delete(id);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("delete cart fail");
    }

    @Override
    public ResultBean getById(Integer id) throws Exception {
        if (id == null){
            return ResultBean.ERROR(10001,"params null");
        }
        Cart byId = cartDao.getById(id);
        return byId!=null?ResultBean.SUCCESS(byId):ResultBean.ERROR("get cart fail");
    }

    @Override
    public ResultBean getAll() throws Exception {
        List<Cart> cartList = cartDao.getAll();
        return cartList!=null?ResultBean.SUCCESS(cartList):ResultBean.ERROR("get all cart fail");
    }

    @Override
    public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
        if (baseQuery == null){
            baseQuery = new CartQuery();
        }
        PageBean<Cart> pageBean = new PageBean<>();
        //查询总记录数
        int count = cartDao.getCount(baseQuery);
        pageBean.setTotalRows(count);

        //计算总页数
        if (count%baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else {
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }

        //查询集合
        List<Cart> cartList = cartDao.getByPage(baseQuery);
        pageBean.setData(cartList);

        return ResultBean.SUCCESS(pageBean);
    }

    //批量删除
    @Override
    public ResultBean batchDel(List<Integer> ids) throws Exception {
        return null;
    }
}
